﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhanMemDoVui.BusinessLogic;
using PhanMemDoVui.BusinessObject;

namespace PhanMemDoVui
{
    public partial class frmExam : Form
    {
        private DataTable deThi = new DataTable();// bo cau hoi dung lam de thi
        private DataTable markTime = new DataTable();// diem so va thoi gian cho moi loai cau, sap theo 1 hang ngang
        private int index = 0;//thu tu cau hoi dang dung trong DataTable deThi
        private int maExam; //ma so cuoc thi hien tai
        private DateTime curTime;

        public frmExam()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.Lamp;
        }

        private void frmExam_Load(object sender, EventArgs e)
        {
            ExamBUS exam = ExamBUS.Instance();
            BankBUS bank = new BankBUS();
            ExamUse use = new ExamUse();
            // xu ly cuoc thi
            maExam = exam.LayMaCuocThi();
            deThi = exam.LayDeThi();
            markTime = bank.Get();
            // xu ly su kien timer
            int time = 0;
            if (deThi.Rows[index]["Difficulty"].ToString() == "easy")
                time = Int32.Parse(markTime.Rows[0]["time_easy"].ToString());
            else if (deThi.Rows[index]["Difficulty"].ToString() == "mode")
                time = Int32.Parse(markTime.Rows[0]["time_mode"].ToString());
            else if (deThi.Rows[index]["Difficulty"].ToString() == "hard")
                time = Int32.Parse(markTime.Rows[0]["time_hard"].ToString());
            else if (deThi.Rows[index]["Difficulty"].ToString() == "vhar")
                time = Int32.Parse(markTime.Rows[0]["time_vhar"].ToString());
            curTime = new DateTime(2011, 5, 25, 0, 0, time);
            timer.Enabled = true;

            //luu lai ma so cuoc thi de chi can goi ham 1 lan
            use.Ma = maExam;
            use.MaCH = Int32.Parse(deThi.Rows[index]["MaCH"].ToString());
            // Khi nap form thi nap luon cau hoi thu nhat
            lab_question.Text = deThi.Rows[index]["Question"].ToString();
            lab_ans1.Text = deThi.Rows[index]["Ans1"].ToString();
            lab_ans2.Text = deThi.Rows[index]["Ans2"].ToString();
            lab_ans3.Text = deThi.Rows[index]["Ans3"].ToString();
            lab_ans4.Text = deThi.Rows[index]["Ans4"].ToString();
            // Luu lai de cho biet da su dung cau hoi nay
            exam.LuuCuocThi(use);
        }

        private void btn_next_Click(object sender, EventArgs e)
        {
            ExamBUS exam = ExamBUS.Instance();
            ScoreBUS score = new ScoreBUS();
            int choose = 0, mark = 0;      
            // Lay dap an ma nguoi dung chon
            if (rad_choose_A.Checked == true)
                choose = 1;
            else if (rad_choose_B.Checked == true)
                choose = 2;
            else if (rad_choose_C.Checked == true)
                choose = 3;
            else if (rad_choose_D.Checked == true)
                choose = 4;
            // lay diem so tuong ung voi cau hoi va muc do kho
            if (deThi.Rows[index]["Difficulty"].ToString() == "easy")
                mark = Int32.Parse(markTime.Rows[0][0].ToString());
            else if (deThi.Rows[index]["Difficulty"].ToString() == "mode")
                mark = Int32.Parse(markTime.Rows[0][1].ToString());
            else if (deThi.Rows[index]["Difficulty"].ToString() == "hard")
                mark = Int32.Parse(markTime.Rows[0][2].ToString());
            else if (deThi.Rows[index]["Difficulty"].ToString() == "vhar")
                mark = Int32.Parse(markTime.Rows[0][3].ToString());
            //Neu nguoi choi chon dung dap an thi cong diem
            if (choose == Int32.Parse(deThi.Rows[index]["R_Ans"].ToString()))
                score.CapNhat(mark, maExam);
            else//Neu nguoi choi chua chon cau hoi thi diem = 0
                score.CapNhat(0, maExam);
            //Neu index = so record trong deThi tuc la da thi xong, chuyen sang form EndExam
            if (index == deThi.Rows.Count - 1)// -1 vi index bat dau tu 0
            {
                timer.Enabled = false;
                exam.KetThucThi();
                frmEndExam end = new frmEndExam();    
                this.Hide();
                end.Show();
            }
            else
            {
                // neu khong thi tang index len 1 cho biet den luot cau hoi nay trong DataTable deThi
                index++;
                // uncheck tat ca radio button
                rad_choose_A.Checked = false;
                rad_choose_B.Checked = false;
                rad_choose_C.Checked = false;
                rad_choose_D.Checked = false;
                // Hien thi cau hoi va cau tra loi
                lab_question.Text = deThi.Rows[index]["Question"].ToString();
                lab_ans1.Text = deThi.Rows[index]["Ans1"].ToString();
                lab_ans2.Text = deThi.Rows[index]["Ans2"].ToString();
                lab_ans3.Text = deThi.Rows[index]["Ans3"].ToString();
                lab_ans4.Text = deThi.Rows[index]["Ans4"].ToString();
                //Luu lai de cho biet cau hoi nay da duoc su dung
                ExamUse use = new ExamUse();
                use.Ma = maExam;
                use.MaCH = Int32.Parse(deThi.Rows[index]["MaCH"].ToString());
                exam.LuuCuocThi(use);
                // xu ly timer
                int time = 0;
                if (deThi.Rows[index]["Difficulty"].ToString() == "easy")
                    time = Int32.Parse(markTime.Rows[0]["time_easy"].ToString());
                else if (deThi.Rows[index]["Difficulty"].ToString() == "mode")
                    time = Int32.Parse(markTime.Rows[0]["time_mode"].ToString());
                else if (deThi.Rows[index]["Difficulty"].ToString() == "hard")
                    time = Int32.Parse(markTime.Rows[0]["time_hard"].ToString());
                else if (deThi.Rows[index]["Difficulty"].ToString() == "vhar")
                    time = Int32.Parse(markTime.Rows[0]["time_vhar"].ToString());
                curTime = new DateTime(2011, 5, 25, 0, 0, time);
                timer.Enabled = true;
            }
        }
        private void btn_savequit_Click(object sender, EventArgs e)
        {
            timer.Enabled = false;
            DialogResult result = MessageBox.Show("Bạn có chắc muốn lưu cuộc thi?", "Lưu cuộc thi", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                UserBUS user = UserBUS.Instance();
                if (user.IsAdmin())
                {
                    frmAdmin admin = new frmAdmin();
                    this.Hide();
                    admin.Show();
                }
                else
                {
                    frmCommonUser comm = new frmCommonUser();
                    this.Hide();
                    comm.Show();
                }
            }
            else
                timer.Enabled = true;
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = new TimeSpan(0, 0, 1);
            // hien thi thoi gian luc chua bi tru (Subtract)
            lab_timer.Show();
            lab_timer.Text = curTime.Minute.ToString() + " : " + curTime.Second.ToString();
            curTime = curTime.Subtract(ts);
            // Hien thi thoi gian tren lab_timer
            lab_timer.Text = curTime.Minute.ToString() + " : " + curTime.Second.ToString();
            // Khi het thoi gian thi xem nhu an nut "Tiep"
            if (curTime.Minute == 0 && curTime.Second == 0)
            {
                timer.Enabled = false;
                //lab_timer.Text = curTime.Minute.ToString() + " : " + curTime.Second.ToString();
                btn_next_Click(sender, e);
            }
            //lab_timer.Hide();          
        }

        private void frmExam_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}
